package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.yxjwzpx.management.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IShipService shipService;
    @Autowired
    private IFaultReportService faultReportService;
    @Autowired
    private IMaintenancePlanService maintenancePlanService;
    @Autowired
    private IRepairCostService repairCostService;
    @Autowired
    private IShipOwnerService shipOwnerService;
    @Autowired
    private IMaintainCompanyService maintainCompanyService;

    @GetMapping("/statistics")
    public AjaxResult getStatistics(HttpServletRequest request) {
        Long ownerId = null;
        Long companyId = null;

        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser user = loginUser.getUser();
        List<SysRole> roles = user.getRoles();
        String roleKey = roles.get(0).getRoleKey();

        if ("OWNER".equals(roleKey)) {
            ownerId = shipOwnerService.selectOwnerIdByUserId(user.getUserId());
        } else if ("COMPANY".equals(roleKey)) {
            companyId = maintainCompanyService.selectCompanyIdByUserId(user.getUserId());
        }

        Map<String, Object> data = new HashMap<>();
        data.put("shipCount", shipService.countShips(ownerId, companyId));
        data.put("faultCount", faultReportService.countThisMonthByUserId(user.getUserId()));
        data.put("ownerCount", shipOwnerService.countOwners());
        data.put("totalPlanCost", maintenancePlanService.sumActualCost(companyId));

        return AjaxResult.success(data);
    }

    @GetMapping("/repair-cost-trend")
    public AjaxResult getRepairCostTrend(HttpServletRequest request) {
        return AjaxResult.success(repairCostService.getMonthlyCostTrend(getCompanyIdIfCompany(request)));
    }

    @GetMapping("/maintenance-plan-cost")
    public AjaxResult getPlanCostList(HttpServletRequest request) {
        return AjaxResult.success(maintenancePlanService.getPlanCostList(getCompanyIdIfCompany(request)));
    }

    @GetMapping("/fault-type-count")
    public AjaxResult getFaultTypeCount(HttpServletRequest request) {
        Long userId = tokenService.getLoginUser(request).getUserId();
        return AjaxResult.success(faultReportService.getFaultTypeDistributionByUserId(userId));
    }


    private Long getOwnerIdIfOwner(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser user = loginUser.getUser();
        return user.getRoles().stream().anyMatch(r -> "OWNER".equals(r.getRoleKey()))
                ? shipOwnerService.selectOwnerIdByUserId(user.getUserId())
                : null;
    }

    private Long getCompanyIdIfCompany(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser user = loginUser.getUser();
        return user.getRoles().stream().anyMatch(r -> "COMPANY".equals(r.getRoleKey()))
                ? maintainCompanyService.selectCompanyIdByUserId(user.getUserId())
                : null;
    }
}
